//
//  NSDate+GetValue.m
//  RenRenZhuan
//
//  Created by leikun on 15/12/28.
//  Copyright © 2015年 leAppSoft. All rights reserved.
//

#import "NSDate+GetValue.h"

@implementation NSDate (GetValue)



/**
 获取中国当前时间
 */
+ (NSDate *)getChinaNowTime
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    return localeDate;
}
/**
 比较该日期是不是今天
 */
+ (BOOL)compareTodayWithOtherDate:(NSDate *)otherDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyyMMdd"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSString *todayString = [formatter stringFromDate:[self getChinaNowTime]];
    NSString *otherDateString = [formatter stringFromDate:otherDate];
    if ([todayString isEqualToString:otherDateString]) {
        return YES;
    }else
    {
        return NO;
    }
}

/**
 时间戳转成时间
 */
+ (NSDate *)getDateForTimeStamp:(NSString *)stamp
{
    
    NSString *newDateString = [stamp substringToIndex:[stamp length]-3];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[newDateString doubleValue]];
    NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
    NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
    return localeDate;
}

/**
 判断今天是周几
 */
+ (NSInteger )dealWithDate:(NSDate  *)tmpDate
{
    //外国将周日作为第一天 也可以设置将周一作为第一天
    //    NSArray *array = @[[NSNull null],@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    NSInteger unitFlags =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unitFlags fromDate:tmpDate];
    
    return [components weekday];
}

/**
 获取日期在这个月的第几天
 */
+ (NSInteger)getNumberOfMonthForThisDay:(NSDate *)thisDay
{
    
    NSCalendar *cal = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    cal.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour;
    NSDateComponents *dateComponent = [cal components:unitFlags fromDate:thisDay];
    return [dateComponent day];
    
}
/**
 获得月份
 */
+ (NSInteger)getMonthOfMonthForThisDay:(NSDate *)thisDay
{
    NSCalendar *cal = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    cal.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *dateComponent = [cal components:unitFlags fromDate:thisDay];
    return [dateComponent month];
    
}
/**
 获得年份
 */
+ (NSInteger)getYearOfMonthForThisDay:(NSDate *)thisDay
{
    
    NSCalendar *cal = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    cal.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *dateComponent = [cal components:unitFlags fromDate:thisDay];
    return [dateComponent year];
    
}


/**
 获取这个月的第一天
 */
+ (NSDate *)getThisMonthFirstDay
{
    NSDate *now = [self getChinaNowTime];
    NSCalendar *cal = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    cal.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    NSDateComponents *comps = [cal
                               components:NSCalendarUnitYear | NSCalendarUnitMonth
                               fromDate:now];
    comps.day = 1;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return firstDay;
}


/**
 获得当前日期以后的时间
 @param number 经过多少天
 */

+ (NSDate *)getDateInAfterDayForSomeDay:(NSInteger)number
{
    NSDate *now = [NSDate getChinaNowTime];
    NSTimeInterval timeInterval = number *60*60*24;
    return [now dateByAddingTimeInterval:timeInterval];
}

/**
 获得这两个日期相隔几天
 @param date 第一个日期
 @param otherDate 第二个日期
 */

+ (NSInteger)getNumberBetweenDate:(NSDate *)date otherDate:(NSDate *)otherDate
{
    
    NSTimeInterval timeInterval =  [date timeIntervalSinceDate:otherDate];
    NSInteger otherDay = timeInterval;
    NSInteger dayInterval = 60*60*24;
    return otherDay%dayInterval;
}
@end
